package com.hdu.careerplan.controller;

import com.hdu.careerplan.enums.StatusCode;
import com.hdu.careerplan.exception.WrongKeyInJwtException;
import com.hdu.careerplan.pojo.dto.AssessmentScoreDTO;
import com.hdu.careerplan.pojo.entity.CompetenceInfo;
import com.hdu.careerplan.pojo.entity.PositionInfo;
import com.hdu.careerplan.pojo.vo.Result;
import com.hdu.careerplan.pojo.vo.ResultData;
import com.hdu.careerplan.service.AssessmentService;
import com.hdu.careerplan.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/assessment")
public class AssessmentController {

    @Resource
    private AssessmentService assessmentService;

    @GetMapping("/position")
    public ResultData getPositionList() {
        List<PositionInfo> positionList = assessmentService.getPositionList();
        if (positionList.isEmpty()) return Result.responseWithNonData(StatusCode.UNKNOWN_ERROR);
        else return Result.responseWithData(StatusCode.SUCCESS, positionList);
    }

    @PostMapping("/interest")
    public ResultData uploadAssessmentInterestScore(@RequestBody List<AssessmentScoreDTO> scoreList, ServletRequest servletRequest) throws WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        assessmentService.uploadAssessmentInterestScore(scoreList, Long.valueOf(userId.toString()));
        return Result.responseWithNonData(StatusCode.SUCCESS);
    }

    @GetMapping("/interest")
    public ResultData getAssessmentInterestScore(ServletRequest servletRequest) throws WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        List<Integer> assessmentInterestScore = assessmentService.getAssessmentInterestScore(Long.valueOf(userId.toString()));
        if (assessmentInterestScore == null) return Result.responseWithNonData(StatusCode.NO_CONTENT);
        return Result.responseWithData(StatusCode.SUCCESS, assessmentInterestScore);
    }

    @GetMapping("/competence")
    public ResultData getCompetenceScore(ServletRequest servletRequest) throws IOException, InterruptedException, WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        List<CompetenceInfo> competenceScore = assessmentService.getCompetenceScore(Long.valueOf(userId.toString()));
        if (competenceScore == null || competenceScore.isEmpty()) {
            return Result.responseWithNonData(StatusCode.NO_CONTENT);
        }
        else return Result.responseWithData(StatusCode.SUCCESS, competenceScore);

    }



}
